from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
from . import service

# Create your views here.
def demo_table(request):
    df,columns, data = service.data_loan()
    return render(request, 'demo/table-demo.html', {'columns':columns, 'data':data})

def demo_echarts(request):
    return render(request, 'demo/echarts-demo.html', {})

def echarts(request):
    
    return render(request, 'demo/echarts.html', {})

def loan_echarts(request):
    static_col = service.static_columns()
    org_corr,new_corr = service.static_corr()  
    return render(request, 'demo/loan_echarts.html', {'org_corr':org_corr,'new_corr':new_corr,'static_col':static_col})


def demo_loan_visualize(request):
    org_corr,new_corr = service.static_corr()  
    return render(request, 'demo/loan_visualize.html', {'org_corr':org_corr,'new_corr':new_corr,})

def demo_loan_training(request):
    df,columns, data = service.data_loan()
    moder_param =service.get_model_param()
    return render(request, 'demo/loan_training.html',{'columns':columns,'moder_param':moder_param})


@csrf_exempt
def demo_api_loan_train_logistic(request):
    modal_param = json.loads(request.POST.get('data'))
    service.train_AdaBoostClassifier(modal_param)
    return HttpResponse('训练完成')


@csrf_exempt
def demo_api_loan_predict_logistic(request):
    loan_data = json.loads(request.POST.get('data'))
    r = service.predict_AdaBoostClassifier(loan_data)    
    return HttpResponse("借" if r else "不借")


@csrf_exempt
def demo_api_loan_train_tree(request):
    modal_param = json.loads(request.POST.get('data'))
    
    service.train_GradientBoostingClassifier(modal_param)
    return HttpResponse('训练完成')


@csrf_exempt
def demo_api_loan_predict_tree(request):
    loan_data = json.loads(request.POST.get('data'))
    r = service.predict_GradientBoostingClassifier(loan_data)
    return HttpResponse("借" if r else "不借")
